package comment

import (
	"github.com/astaxie/beego/orm"
	"qycms/controllers"
	"qycms/models/comment"
	"qycms/untils"
	"strings"
)

type CommentController struct {
	controllers.BaseController
}
/**
 * 评论主页面
 */
func (this *CommentController) Index()  {
	 this.TplName = "comment/index.html"
}
/**
 * 评论数据
 */
func (this *CommentController) CommentList()  {
	//分页计算
	currentPage,err := this.GetInt("page",1)
	pagePerNum,_ := this.GetInt("limit",20)
	if(err != nil ){
		currentPage = 1
	}
	offsetNum := pagePerNum * (currentPage - 1)

	//数据查询
	var count int64 = 0
	comment := []comment.Comment{}

	O := orm.NewOrm()
	q := O.QueryTable("sys_comment")

	keyword := this.GetString("key")
	if keyword != ""{
		q = q.Filter("content__icontains",keyword)
	}

	count,_ = q.Filter("is_delete",0).Count()
	q.Filter("is_delete",0).Limit(pagePerNum).Offset(offsetNum).OrderBy("-create_time").RelatedSel().All(&comment)

	this.Data["json"] = untils.ListJson(count,comment)
	this.ServeJSON()
	this.StopRun()
}

/**
 * 删除评论
 */
func (this *CommentController) DelComment()  {
	//接收参数
	ids := this.GetString("ids");
	new_ids := ids[:len(ids)]
	id_arr := strings.Split(new_ids,",")

	//数据更新
	o := orm.NewOrm()
	qs := o.QueryTable("sys_comment")
	for _,v :=range id_arr{
		id_int := untils.StrToInt(v)
		qs.Filter("id",id_int).Update(orm.Params{
			"is_delete":1,
		})
	}
	this.Suc(nil)
}